package com.sttg.csp.registration.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import com.sttg.csp.common.exception.BizException;
import com.sttg.csp.common.util.LDAPUtil;

public class BDAdminManageDAO {

	private static final BDAdminManageDAO instance = new BDAdminManageDAO();   
	
	/** 
	 * Private the default constructor.
	 */   
	private BDAdminManageDAO() {}    
   
	/** 
	 * Static factory methods.
	 */   
	public static BDAdminManageDAO getInstance() {
		return instance;    
	}    
	
	/**
	 * Search a BD admin by userid.
	 * @param uid
	 * @return
	 */
	public Map queryBDAdminByUserID(String uid) throws BizException {
		Map<String, String> user = new HashMap<String, String>();
		try {
			SearchControls controls = new SearchControls();
			controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
			Attributes attrs = LDAPUtil.getDirContext().getAttributes("uid=" + uid + "," + LDAPUtil.BD_ADMIN_RDP + "," + LDAPUtil.BASE_DN);
			String userid = LDAPUtil.getAttribute(attrs, "uid");
			String cn = LDAPUtil.getAttribute(attrs, "cn");
			String departmentnumber = LDAPUtil.getAttribute(attrs, "departmentnumber");
			String mail = LDAPUtil.getAttribute(attrs, "mail");
			String telephoneNumber = LDAPUtil.getAttribute(attrs, "telephonenumber");
			user.put("DP_USER_ID", userid);
			user.put("BD_ADMIN_NAME", cn);
			user.put("DEPARTMENT", departmentnumber);
			user.put("MAIL", mail);
			user.put("TELEPHONE_NUMBER", telephoneNumber);
		} catch (Exception e) {
			throw new BizException(e);
		} finally {
			LDAPUtil.closeConnection();
		}
		return user;
	}
	
	/**
	 * Search all B/D admins.
	 * @param param
	 * @return all B/D admins list.
	 */
	public List queryBDAdminList(Map param) throws BizException  {
		List<Object> list = new ArrayList<Object>();
		try {
			SearchControls controls = new SearchControls();
			controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
			NamingEnumeration<SearchResult> answer = LDAPUtil.getDirContext().search(LDAPUtil.BD_ADMIN_RDP + "," + LDAPUtil.BASE_DN, "cn=*", controls); // 查询所有用户

			while (answer.hasMore()) {
				Map<String, String> user = new HashMap<String, String>();
				SearchResult result = (SearchResult) answer.next();
				//System.out.println(result.getName());
				System.out.println(result.toString());
				Attributes attrs = result.getAttributes();
				String userid = LDAPUtil.getAttribute(attrs, "uid");
				String cn = LDAPUtil.getAttribute(attrs, "cn");
				String departmentnumber = LDAPUtil.getAttribute(attrs, "departmentnumber");
				user.put("DP_USER_ID", userid);
				user.put("BD_ADMIN_NAME", cn);
				user.put("DEPARTMENT", departmentnumber);
				list.add(user);
			}
		} catch (Exception e) {
			throw new BizException(e);
		} finally {
			LDAPUtil.closeConnection();
		}
		return list;
	}

	// insert a B/D admin
	public void doInsertBDAdmin(Map param) throws BizException {
		String uid = (String) param.get("DP_USER_ID");
		String commonName = (String) param.get("BD_ADMIN_NAME");
		String departmentNumber = (String) param.get("DEPARTMENT");
		String mail = (String) param.get("MAIL");
		String telephoneNumber = (String) param.get("TELEPHONE_NUMBER");
		
		Attributes attrs = new BasicAttributes();

		Attribute objclass = new BasicAttribute("objectclass");
		objclass.add("top");
		objclass.add("person");
		objclass.add("organizationalPerson");
		objclass.add("inetorgperson");
		attrs.put(objclass);
		
		LDAPUtil.putAttribute(attrs, "uid", uid);
		LDAPUtil.putAttribute(attrs, "cn", commonName);
		LDAPUtil.putAttribute(attrs, "sn", "lastname");
		LDAPUtil.putAttribute(attrs, "departmentNumber", departmentNumber);
		LDAPUtil.putAttribute(attrs, "mail", mail);
		LDAPUtil.putAttribute(attrs, "telephonenumber", telephoneNumber);

		System.out.println(attrs);
		try {
			LDAPUtil.getDirContext().bind("uid=" + uid + "," + LDAPUtil.BD_ADMIN_RDP + "," + LDAPUtil.BASE_DN, null, attrs);
		} catch (NamingException e) {
			e.printStackTrace();
			throw new BizException(e);
		} finally {
			LDAPUtil.closeConnection();
		}
	}
	
	/**
	 * Modify a B/D admin info.
	 * @param param
	 * @throws BizException
	 */
	public void doUpdateBDAdmin(Map param) throws BizException {
		String uid = (String) param.get("DP_USER_ID");
		String commonName = (String) param.get("BD_ADMIN_NAME");
		String departmentNumber = (String) param.get("DEPARTMENT");
		String mail = (String) param.get("MAIL");
		String telephoneNumber = (String) param.get("TELEPHONE_NUMBER");
		
		Attributes attrs = new BasicAttributes();

		LDAPUtil.putAttribute(attrs, "cn", commonName);
		LDAPUtil.putAttribute(attrs, "sn", "lastname");
		LDAPUtil.putAttribute(attrs, "departmentNumber", departmentNumber);
		LDAPUtil.putAttribute(attrs, "mail", mail);
		LDAPUtil.putAttribute(attrs, "telephonenumber", telephoneNumber);

		System.out.println(attrs);
		try {
			LDAPUtil.getDirContext().modifyAttributes("uid=" + uid + "," + LDAPUtil.BD_ADMIN_RDP + "," + LDAPUtil.BASE_DN, DirContext.REPLACE_ATTRIBUTE, attrs);
		} catch (NamingException e) {
			throw new BizException(e);
		} finally {
			LDAPUtil.closeConnection();
		}
	}
	
	/**
	 * Delete a B/D admin info.
	 * @param param
	 * @throws BizException
	 */
	public void doDeleteBDAdmin(Map param) throws BizException {
		String uid = (String) param.get("DP_USER_ID");
		try {
			LDAPUtil.getDirContext().destroySubcontext("uid=" + uid + "," + LDAPUtil.BD_ADMIN_RDP + "," + LDAPUtil.BASE_DN);
		} catch (NamingException e) {
			throw new BizException(e);
		} finally {
			LDAPUtil.closeConnection();
		}
	}
	
	public static void main(String[] args) {
		try {

			//List list = findBDAdminList("");
			//System.out.println(list);
			// String user = "orcladmin";
			// Attributes attrs = findUser(user);
			//
			// System.out.println("First Name: " + getAttribute(attrs, "givenname"));
			// System.out.println("Last Name: " + getAttribute(attrs, "sn"));
			// System.out.println("Common Name: " + getAttribute(attrs, "cn"));
			// System.out.println("User ID: " + getAttribute(attrs, "uid"));
			// System.out.println("E-Mail: " + getAttribute(attrs, "mail"));
			// System.out.println("Phone: " + getAttribute(attrs, "telephonenumber"));
			// System.out.println("Fax: " + getAttribute(attrs, "facsimiletelephonenumber"));
			// System.out.println("List completed.");
			// System.out.println("-----------------------------n");

		} catch (Exception e) {
			System.out.println(e);
		}
	}

}